package leetcode.动态规划;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/8/16 20:04
 * @description：https://leetcode-cn.com/problems/jump-game/
 */
public class 跳跃游戏 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.canJump(new int[]{3,2,1,0,4}));
    }

    class Solution {
        public boolean canJump(int[] nums) {
            int n = nums.length;
            if (n == 0) return true;
            int mostRight = nums[0];
            for (int i = 0; i < n; i++) {
                if (i <= mostRight) {
                    mostRight = Math.max(mostRight, nums[i] + i);
                }
                if (mostRight >= n - 1) {
                    return true;
                }
            }
            return false;
        }
    }

}
